package com.ht.baselibs.ui.base;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.ht.baselibs.R;
import com.ht.baselibs.bean.OperatorBean;
import com.ht.baselibs.ui.adapter.OperatorAdapter;
import com.ht.baselibs.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import de.greenrobot.event.EventBus;

public abstract class BaseOperatorListFragment extends BaseFragment implements AdapterView.OnItemClickListener {
    protected ListView mListView;
    protected List<OperatorBean> mDataList;
    protected OperatorAdapter mOperatorAdapter;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected View initContentView() {
        return UIUtils.inflate(R.layout.view_list_view);
    }

    @Override
    protected void initChildView() {


        mListView = findViewById(R.id.lv_view_list);
        EventBus.getDefault().register(this);
        View headerView = getHeaderView();
        if (headerView != null) {
            mListView.addHeaderView(headerView);
        }

    }

    protected abstract View getHeaderView();

    @Override
    protected void setChildViewListener() {
        mListView.setOnItemClickListener(this);
    }

    @Override
    protected void load() {
        mDataList = new ArrayList<>();
        loadData();
        mOperatorAdapter = new OperatorAdapter(mContext, mDataList);
        mListView.setAdapter(mOperatorAdapter);
    }

    protected abstract void loadData();

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        position = position - mListView.getHeaderViewsCount();// 此时的position是加上了header的
        try {
            //TODO 这里问题 没时间，暂不处理  就是listview增加头部position的问题
            OperatorBean operator = mDataList.get(position);
            if (operator.onItemClickListener != null) {
                operator.onItemClickListener.onClick(false);
            }
        } catch (Exception e) {
            e.getMessage();
        }
    }

    protected void add(int iconRes, String title, OperatorBean.OnItemClickListener onItemClickListener) {
        add(false, iconRes, title, onItemClickListener);
    }

    protected void add(boolean isStart, int iconRes, String title, OperatorBean.OnItemClickListener onItemClickListener) {
        mDataList.add(new OperatorBean(isStart, iconRes, title, onItemClickListener));
    }

    protected void add(String title, OperatorBean.OnItemClickListener onItemClickListener) {
        add(false, title, onItemClickListener);
    }

    protected void add(boolean isStart, String title, OperatorBean.OnItemClickListener onItemClickListener) {
        mDataList.add(new OperatorBean(isStart, title, onItemClickListener));
    }

    protected void add(String title, boolean isCheckbox, boolean defaultValue, OperatorBean.OnItemClickListener onItemClickListener) {
        add(false, title, isCheckbox, defaultValue, onItemClickListener);
    }

    protected void add(boolean isStart, String title, boolean isCheckbox, boolean defaultValue, OperatorBean.OnItemClickListener onItemClickListener) {
        mDataList.add(new OperatorBean(isStart, title, isCheckbox, defaultValue, onItemClickListener));
    }

    protected void add(String title, String detail, OperatorBean.OnItemClickListener onItemClickListener) {
        add(false, 0, title, detail, onItemClickListener);
    }

    protected void add(int icon, String title, String detail, OperatorBean.OnItemClickListener onItemClickListener) {

        add(false, icon, title, detail, onItemClickListener);
    }

    protected void add(boolean isStart, int icon, String title, String detail, OperatorBean.OnItemClickListener onItemClickListener) {
        mDataList.add(new OperatorBean(isStart, icon, title, detail, onItemClickListener));
    }

    protected void add(String title, boolean isCenter, OperatorBean.OnItemClickListener onItemClickListener) {
        add(false, title, isCenter, onItemClickListener);
    }

    protected void add(boolean isStart, String title, boolean isCenter, OperatorBean.OnItemClickListener onItemClickListener) {
        mDataList.add(new OperatorBean(isStart, title, isCenter, onItemClickListener));
    }

    protected void add(boolean isHideRightRes,int hideColor,int icon,String title,String detail, OperatorBean.OnItemClickListener onItemClickListener) {
        add(false,isHideRightRes,hideColor,icon,title, detail, onItemClickListener);
    }

    protected void add(boolean isStart,boolean isHideRightRes,int hideColor,int icon,String title,String detail, OperatorBean.OnItemClickListener onItemClickListener) {
        mDataList.add(new OperatorBean(isStart, isHideRightRes,hideColor,icon,title,detail, onItemClickListener));
    }

    public void onEventMainThread(Object object) {
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);//反注册EventBus
    }
}